
#include "main.h"

#define   DEBOUNCE			4	  /* how many times check the key every 10mS*/

//#define   PIN_SIM_SWITCH	RB4

/* Keyboard pins: rows and columns*/
/* columns */
#define   KB_C0	RF2
#define   KB_C1	RF4
#define   KB_C2	RF6
#define   KB_C3	RF0
/* rows */
#define   KB_R0	RG0
#define   KB_R1	RF1
#define   KB_R2	RF3
#define   KB_R3	RF5
#define   KB_R4	RF7


KEYPAD near Keypad;
unsigned char near InputsState = 0;
unsigned char near PressedKey = 0;

/* debounce counter */
unsigned char HookDeb = 0;
unsigned char OnKeyDeb = 0;
unsigned char dsi = 0;	
unsigned char dse = 0;
unsigned char d0 = 0;
unsigned char d1 = 0;
unsigned char d2 = 0;
unsigned char d3 = 0;
unsigned char d4 = 0;
unsigned char d5 = 0;
unsigned char d6 = 0;
unsigned char d7 = 0;
unsigned char d8 = 0;
unsigned char d9 = 0;
unsigned char dP = 0;
unsigned char dS = 0;
unsigned char dM = 0;
unsigned char dC = 0;
unsigned char dO = 0;


void Keypad_Init( void )
{
	KB_C0 = KB_C1 = KB_C2 = KB_C3 = HIGH;
	Keypad.Keys = 0; //0xFFFFFFFF;
	Keypad.Key.sON_OFF = 1;
	//RBPU = 0;						  /* 0 -> PORTB pull-ups are enabled by individual port latch values */
	//TRISB = 0b11111111;
	TRISF = 0b10101011;
	TRISG = 0b11111111;
}
 
void Ext_SIM_Block( void )
{
	PIN_EXTERNAL_SIM = 1;		  /* Block external SIM, first registration with SIM internal only! */
	PIN_EXTERNAL_SIM_DIR = OUTPUT;
}
 
void Ext_SIM_Unblock( void )
{
	PIN_EXTERNAL_SIM_DIR = INPUT;
}

unsigned char OnKeyDeb_val = 250;

void Drive_Keypad( void )
{
	//static unsigned char count = DEBOUNCE_PERIOD;

	PressedKey = InputsState = 0;

	if ( RC_PressedKey )			  /* Remote Control */
	{
		PressedKey = RC_PressedKey;
		RC_PressedKey = 0;
		return ;
	}

	

	/* ON/OFF key */
	if ( ON_OFF_STATE == LOW )
	{
		if ( PIN_ON_OFF ) 
		{	
			if ( OnKeyDeb > OnKeyDeb_val )
			{
				ON_OFF_STATE = HIGH;
				PressedKey = KEY_ON_OFF;
				OnKeyDeb = 0;
			}	
			else OnKeyDeb++;
		}
		else	OnKeyDeb = 0;
	}
	else
	{
		if ( !PIN_ON_OFF ) 
		{	
			if ( OnKeyDeb > DEBOUNCE<<1 )
			{
				ON_OFF_STATE = LOW;
				OnKeyDeb = 0;
			}	
			else OnKeyDeb++;
		}
		else	OnKeyDeb = 0;
	}


	/* =========== Buttons Check ============= */
	
	/* Check: '1', '4', '7', '*', 'MENU'  */
	KB_C2 = LOW;
	
	/* === Key '1'=== */
	if ( KEY.ONE == LOW )
	{
		if ( KB_R4 ) 
		{	
			if ( d1 > DEBOUNCE )
			{
				KEY.ONE = HIGH;
				d1 = 0;
			}	
			else d1++;
		}
		else	d1 = 0;
	}
	else
	{
		if ( !KB_R4 ) 
		{	
			if ( d1 > DEBOUNCE )
			{
				KEY.ONE = LOW;
				PressedKey = '1';
			  	d1 = 0;
			}	
			else d1++;
		}
		else	d1 = 0;
	}

	/* === Key '4' === */
	if ( KEY.FOUR == LOW )
	{
		if ( KB_R3 ) 
		{	
			if ( d4 > DEBOUNCE )
			{
				KEY.FOUR = HIGH;
				d4 = 0;
			}	
			else d4++;
		}
		else	d4 = 0;
	}
	else
	{
		if ( !KB_R3 ) 
		{	
			if ( d4 > DEBOUNCE )
			{
				KEY.FOUR = LOW;
				PressedKey = '4';
			  	d4 = 0;
			}	
			else d4++;
		}
		else	d4 = 0;
	}

   /* === Key '7' === */
	if ( KEY.SEVEN == LOW )
	{
		if ( KB_R2 ) 
		{	
			if ( d7 > DEBOUNCE )
			{
				KEY.SEVEN = HIGH;
				d7 = 0;
			}	
			else d7++;
		}
		else	d7 = 0;
	}
	else
	{
		if ( !KB_R2 ) 
		{	
			if ( d7 > DEBOUNCE )
			{
				KEY.SEVEN = LOW;
				PressedKey = '7';
			  	d7 = 0;
			}	
			else d7++;
		}
		else	d7 = 0;
	}

   /* === Key '*' === */
	if ( KEY.STAR == LOW )
	{
		if ( KB_R1 ) 
		{	
			if ( dS > DEBOUNCE )
			{
				KEY.STAR = HIGH;
				dS = 0;
			}	
			else dS++;
		}
		else	dS = 0;
	}
	else
	{
		if ( !KB_R1 ) 
		{	
			if ( dS > DEBOUNCE )
			{
				KEY.STAR = LOW;
				PressedKey = '*';
			  	dS = 0;
			}	
			else dS++;
		}
		else	dS = 0;
	}

   /* === Key 'MENU' === */
	if ( KEY.MENU == LOW )
	{
		if ( KB_R0 ) 
		{	
			if ( dM > DEBOUNCE )
			{
				KEY.MENU = HIGH;
				dM = 0;
			}	
			else dM++;
		}
		else	dM = 0;
	}
	else
	{
		if ( !KB_R0 ) 
		{	
			if ( dM > DEBOUNCE )
			{
				KEY.MENU = LOW;
				PressedKey = KEY_MENU;
			  	dM = 0;
			}	
			else dM++;
		}
		else	dM = 0;
	}

	KB_C2 = HIGH;
	/* End of KB_C2 */

	/* Check: '2', '5', '8', 'CLEAR' */
	KB_C1 = LOW;
	/* === Key '2' ==== */
	if ( KEY.TWO == LOW )
	{
		if ( KB_R4 ) 
		{	
			if ( d2 > DEBOUNCE )
			{
				KEY.TWO = HIGH;
				d2 = 0;
			}	
			else d2++;
		}
		else	d2 = 0;
	}
	else
	{
		if ( !KB_R4 ) 
		{	
			if ( d2 > DEBOUNCE )
			{
				KEY.TWO = LOW;
				PressedKey = '2';
			  	d2 = 0;
			}	
			else d2++;
		}
		else	d2 = 0;
	}

	/* ==== Key '5' ===== */
	if ( KEY.FIVE == LOW )
	{
		if ( KB_R3 ) 
		{	
			if ( d5 > DEBOUNCE )
			{
				KEY.FIVE = HIGH;
				d5 = 0;
			}	
			else d5++;
		}
		else	d5 = 0;
	}
	else
	{
		if ( !KB_R3 ) 
		{	
			if ( d5 > DEBOUNCE )
			{
				KEY.FIVE = LOW;
				PressedKey = '5';
			  	d5 = 0;
			}	
			else d5++;
		}
		else	d5 = 0;
	}

	/* === Key: '8' === */
	if ( KEY.EIGHT == LOW )
	{
		if ( KB_R2 ) 
		{	
			if ( d8 > DEBOUNCE )
			{
				KEY.EIGHT = HIGH;
				d8 = 0;
			}	
			else d8++;
		}
		else	d8 = 0;
	}
	else
	{
		if ( !KB_R2 ) 
		{	
			if ( d8 > DEBOUNCE )
			{
				KEY.EIGHT = LOW;
				PressedKey = '8';
			  	d8 = 0;
			}	
			else d8++;
		}
		else	d8 = 0;
	}
	/* === Key: '0' ==== */
	if ( KEY.ZERO == LOW )
	{
		if ( KB_R1 ) 
		{	
			if ( d0 > DEBOUNCE )
			{
				KEY.ZERO = HIGH;
				d0 = 0;
			}	
			else d0++;
		}
		else	d0 = 0;
	}
	else
	{
		if ( !KB_R1 ) 
		{	
			if ( d0 > DEBOUNCE )
			{
				KEY.ZERO = LOW;
				PressedKey = '0';
			  	d0 = 0;
			}	
			else d0++;
		}
		else	d0 = 0;
	}

	/* === Key: 'CLEAR' === */
	if ( KEY.CLEAR == LOW )
	{
		if ( KB_R0 ) 
		{	
			if ( dC > DEBOUNCE )
			{
				KEY.CLEAR = HIGH;
				dC = 0;
			}	
			else dC++;
		}
		else	dC = 0;
	}
	else
	{
		if ( !KB_R0 ) 
		{	
			if ( dC > DEBOUNCE )
			{
				KEY.CLEAR = LOW;
				PressedKey = KEY_CLEAR;
			  	dC = 0;
			}	
			else dC++;
		}
		else	dC = 0;
	}

	KB_C1 = HIGH;
	/* End of KB_C1 */

	/* Check: '3', '6', '9', '#', OK */
	KB_C0 = LOW;
	/* === Key: '3' === */
	if ( KEY.THREE == LOW )
	{
		if ( KB_R4 ) 
		{	
			if ( d3 > DEBOUNCE )
			{
				KEY.THREE = HIGH;
				d3 = 0;
			}	
			else d3++;
		}
		else	d3 = 0;
	}
	else
	{
		if ( !KB_R4 ) 
		{	
			if ( d3 > DEBOUNCE )
			{
				KEY.THREE = LOW;
				PressedKey = '3';
			  	d3 = 0;
			}	
			else d3++;
		}
		else	d3 = 0;
	}
		/* === Key: '6' === */
	if ( KEY.SIX == LOW )
	{
		if ( KB_R3 ) 
		{	
			if ( d6 > DEBOUNCE )
			{
				KEY.SIX = HIGH;
				d6 = 0;
			}	
			else d6++;
		}
		else	d6 = 0;
	}
	else
	{
		if ( !KB_R3 ) 
		{	
			if ( d6 > DEBOUNCE )
			{
				KEY.SIX = LOW;
				PressedKey = '6';
			  	d6 = 0;
			}	
			else d6++;
		}
		else	d6 = 0;
	}
	/* === Key: '9' === */
	if ( KEY.NINE == LOW )
	{
		if ( KB_R2 ) 
		{	
			if ( d9 > DEBOUNCE )
			{
				KEY.NINE = HIGH;
				d9 = 0;
			}	
			else d9++;
		}
		else	d9 = 0;
	}
	else
	{
		if ( !KB_R2 ) 
		{	
			if ( d9 > DEBOUNCE )
			{
				KEY.NINE = LOW;
				PressedKey = '9';
			  	d9 = 0;
			}	
			else d9++;
		}
		else	d9 = 0;
	}
	/* === Key: '#' === */
	if ( KEY.POUND == LOW )
	{
		if ( KB_R1 ) 
		{	
			if ( dP > DEBOUNCE )
			{
				KEY.POUND = HIGH;
				dP = 0;
			}	
			else dP++;
		}
		else	dP = 0;
	}
	else
	{
		if ( !KB_R1 ) 
		{	
			if ( dP > DEBOUNCE )
			{
				KEY.POUND = LOW;
				PressedKey = '#'; //KEY_POUND;
			  	dP = 0;
			}	
			else dP++;
		}
		else	dP = 0;
	}
	/* === Key: 'OKAY' === */
	if ( KEY.OKAY == LOW )
	{
		if ( KB_R0 ) 
		{	
			if ( dO > DEBOUNCE )
			{
				KEY.OKAY = HIGH;
				dO = 0;
			}	
			else dO++;
		}
		else	dO = 0;
	}
	else
	{
		if ( !KB_R0 ) 
		{	
			if ( dO > DEBOUNCE )
			{
				KEY.OKAY = LOW;
				PressedKey = KEY_OKAY;
			  	dO = 0;
			}	
			else dO++;
		}
		else	dO = 0;
	}

	KB_C0 = HIGH;
	
	/* =========== INPUTS =========== */

	/* Hookset */
	if ( HOOK_STATE == LOW )
	{
		if ( PIN_HOOK ) 
		{	
			if ( HookDeb > DEBOUNCE )
			{
				HOOK_STATE = HIGH;
				InputsState = HOOK_OFF;
				HookDeb = 0;
				return; 
			}	
			else HookDeb++;
		}
		else	HookDeb = 0;
	}
	else
	{
		if ( !PIN_HOOK ) 
		{	
			if ( HookDeb > DEBOUNCE )
			{
				HOOK_STATE = LOW;
				InputsState = HOOK_ON;
			  	HookDeb = 0;
				return; 
			}	
			else HookDeb++;
		}
		else	HookDeb = 0;
	}
 
	/* SIM card INTERNAL */
	if ( KEY.SIM_INT == LOW )
	{
		if ( PIN_INTERNAL_SIM ) 
		{	
			if ( dsi > DEBOUNCE )
			{
				KEY.SIM_INT = HIGH;
				InputsState = SIM_INTERNAL_OUT;
				PIN_SIM_ACTIVATE = 0; 
				dsi = 0;
			}	
			else dsi++;
		}
		else	dsi = 0;
	}
	else
	{
		if ( !PIN_INTERNAL_SIM ) 
		{	
			if ( dsi > DEBOUNCE )
			{
				KEY.SIM_INT = LOW;
				InputsState = SIM_INTERNAL_IN;
				PIN_SIM_ACTIVATE = 1; 
				dsi = 0;
			}	
			else dsi++;
		}
		else	dsi = 0;
	}

	/* SIM card EXTERNAL */
	if ( KEY.SIM_EXT == LOW )
	{
		if ( PIN_EXTERNAL_SIM )
		{
			if ( dse > 10 )
			{
				KEY.SIM_EXT = HIGH;
				InputsState = SIM_EXTERNAL_OUT;
				dse = 0;
			}
			else dse++;
		}
		else	dse = 0;
	}
	else
	{
		if ( !PIN_EXTERNAL_SIM )
		{
			if ( dse > 10 )
			{
				KEY.SIM_EXT = LOW;
				InputsState = SIM_EXTERNAL_IN;
				dse = 0;
			}
			else dse++;
		}
		else	dse = 0;
	}
}


